﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Newtonsoft.Json;
using System.Web.Services;

namespace RN.ERP.Web.Exam
{
    /// <summary>
    /// 以企业为单位报名，区别于BmList.aspx
    /// </summary>
    public partial class BmCorpList : BasePage
    {
        private string CurPageCode = "BM";
        private string CurPage = "BmList.aspx";
        public string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }
        public string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        public string RoleType
        {
            get
            {
                return UserInfo.RoleType;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////绑定期次下拉
                BindQCDDL();
                if (RoleType == "Manager")
                {
                    change.Visible = edit.Visible = false;
                    RadGrid1.Columns[2].Visible = RadGrid1.Columns[3].Visible = true;
                    ddlQC.SelectedValue = Request.QueryString["qc"];
                    spReturn.Visible = true;
                }
                ////地区号，主管端根据地区筛选数据
                hidArea.Value = Request.QueryString["areacode"];
                string title = Request.QueryString["title"];
                if (!string.IsNullOrEmpty(title))
                {
                    lbAreaTitle.Text = title + "地区考试报名汇总";
                }
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void ddlQC_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }

        private void BindData()
        {
            RadGrid1.AllowCustomPaging = true;
            int pageCount = RadGrid1.CurrentPageIndex;
            int pageSize = RadGrid1.PageSize;
            int qc = 0;
            int.TryParse(ddlQC.SelectedValue, out qc);
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                var bm = client.GetExamBMList(CurPageCode, UserInfo.RoleId, CurPage, AppID, qc, hidArea.Value, "",0, pageCount + 1, pageSize, out count);
                if (bm != null)
                {
                    var exam = JsonConvert.DeserializeObject<List<ModelService.ExamInfo>>(bm);
                    RadGrid1.VirtualItemCount = count;
                    RadGrid1.DataSource = exam;
                }
            }
        }

        private void BindQCDDL()
        {
            ddlQC.DataSource = GetQC(RoleId, AppID, false);
            ddlQC.DataValueField = "ID";
            ddlQC.DataTextField = "Title";
            ddlQC.DataBind();
            ddlQC.Items.Insert(0, new ListItem("--请选择--", ""));
        }

        /// <summary>
        /// 获取期次
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="appid"></param>
        /// <param name="check">是否判断在考试范围内</param>
        /// <returns></returns>
        private static List<ModelService.Exam_QC> GetQC(string roleid, string appid, bool check)
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                var qc = JsonConvert.DeserializeObject<List<ModelService.Exam_QC>>(client.GetExamQCList
                       ("QC", roleid, "BmQCList.aspx", appid, 0, 0, out count));
                var list = qc;
                if (check)
                {
                    list = qc.Where(m => m.StartDate <= DateTime.Parse(DateTime.Now.ToShortDateString())
                       && m.EndDate >= DateTime.Parse(DateTime.Now.ToShortDateString())).ToList();
                }
                return list;
            }
        }

        [WebMethod]
        public static string CheckQC(object id, object roleId, object appId)
        {
            ////判断期次是否在考试报名时间内
            string ret = null;
            int qcId = 0;
            int.TryParse(id.ToString(), out qcId);
            var list = GetQC(roleId.ToString(), appId.ToString(), true);
            ret = JsonConvert.SerializeObject(new { Message = list.Select(m => m.ID).ToArray().Contains(qcId) });
            return ret;
        }
    }
}